package io.clayton.miep.utils;

import java.util.regex.Pattern;

/**
 * 判断序列字符是否为空、非空
 * 判断一组序列字符是否全部为空、非空、包含空、包含非空
 * <code>Null</code>：没有指向对象
 * <code>Empty</code>：长度为0的序列字符对象
 * <code>Blank</code>：仅有空格的序列字符对象
 */
public class StringUtil {
	
	private static final Pattern BLANK_PATTERN = Pattern.compile("^\\s*$");
	
	/**
	 * 判断序列字符是否为空
	 * @param seq
	 * @return
	 */
	public static boolean isNull(CharSequence seq) {
		return null == seq;
	}
	
	/**
	 * 判断序列字符是否为非空
	 * @param seq
	 * @return
	 */
	public static boolean isNotNull(CharSequence seq) {
		return !isNull(seq);
	}
	
	/**
	 * 判断一组序列字符是否全部为空
	 * @param seqs
	 * @return
	 */
	public static boolean allIsNull(CharSequence... seqs) {
		boolean flag = true;
		for (CharSequence seq : seqs) {
			flag = isNull(seq);
			if (!flag) {
				break;
			}
		}
		return flag;
	}
	
	/**
	 * 判断一组序列字符是否全部为非空
	 * @param seqs
	 * @return
	 */
	public static boolean allIsNotNull(CharSequence... seqs) {
		boolean flag = true;
		for (CharSequence seq : seqs) {
			flag = isNotNull(seq);
			if (!flag) {
				break;
			}
		}
		return flag;
	}
	
	/**
	 * 判断一组序列字符是否包含空序列字符
	 * @param seqs
	 * @return
	 */
	public static boolean hasNull(CharSequence... seqs) {
		return !allIsNotNull(seqs);
	}
	
	/**
	 * 判断一组序列字符是否包含非空序列字符
	 * @param seqs
	 * @return
	 */
	public static boolean hasNotNull(CharSequence... seqs) {
		return !allIsNull(seqs);
	}

	/**
	 * 判断序列字符是否为空
	 * @param seq
	 * @return
	 */
	public static boolean isEmpty(CharSequence seq) {
		if (isNull(seq)) {
			return true;
		}
		return seq.length()==0;
	}
	
	/**
	 * 判断序列字符是否为非空
	 * @param seq
	 * @return
	 */
	public static boolean isNotEmpty(CharSequence seq) {
		return !isEmpty(seq);
	}


	/**
	 * 判断一组序列字符是否全部为空
	 * @param seqs
	 * @return
	 */
	public static boolean allIsEmpty(CharSequence... seqs) {
		boolean flag = true;
		for (CharSequence seq : seqs) {
			flag = isEmpty(seq);
			if (!flag) {
				break;
			}
		}
		return flag;
	}
	
	/**
	 * 判断一组序列字符是否全部为非空
	 * @param seqs
	 * @return
	 */
	public static boolean allIsNotEmpty(CharSequence... seqs) {
		boolean flag = true;
		for (CharSequence seq : seqs) {
			flag = isNotEmpty(seq);
			if (!flag) {
				break;
			}
		}
		return flag;
	}
	
	/**
	 * 判断一组序列字符是否包含空序列字符
	 * @param seqs
	 * @return
	 */
	public static boolean hasEmpty(CharSequence... seqs) {
		return !allIsNotEmpty(seqs);
	}
	
	/**
	 * 判断一组序列字符是否包含非空序列字符
	 * @param seqs
	 * @return
	 */
	public static boolean hasNotEmpty(CharSequence... seqs) {
		return !allIsEmpty(seqs);
	}
	
	/**
	 * 判断序列字符是否为空
	 * @param seq
	 * @return
	 */
	public static boolean isBlank(CharSequence seq) {
		if (isEmpty(seq)) {
			return true;
		}
		return BLANK_PATTERN.matcher(seq).find();
	}
	
	/**
	 * 判断序列字符是否为非空
	 * @param seq
	 * @return
	 */
	public static boolean isNotBlank(CharSequence seq) {
		return !isBlank(seq);
	}

	/**
	 * 判断一组序列字符是否全部为空
	 * @param seqs
	 * @return
	 */
	public static boolean allIsBlank(CharSequence... seqs) {
		boolean flag = true;
		for (CharSequence seq : seqs) {
			flag = isBlank(seq);
			if (!flag) {
				break;
			}
		}
		return flag;
	}
	
	/**
	 * 判断一组序列字符是否全部为非空
	 * @param seqs
	 * @return
	 */
	public static boolean allIsNotBlank(CharSequence... seqs) {
		boolean flag = true;
		for (CharSequence seq : seqs) {
			flag = isNotBlank(seq);
			if (!flag) {
				break;
			}
		}
		return flag;
	}
	
	/**
	 * 判断一组序列字符是否包含空序列字符
	 * @param seqs
	 * @return
	 */
	public static boolean hasBlank(CharSequence... seqs) {
		return !allIsNotBlank(seqs);
	}
	
	/**
	 * 判断一组序列字符是否包含非空序列字符
	 * @param seqs
	 * @return
	 */
	public static boolean hasNotBlank(CharSequence... seqs) {
		return !allIsBlank(seqs);
	}
	
}